package com.dongdongshop.service;

import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbContentServiceImpl implements TbContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> selectAll() {
        List<TbContent> tbContents = tbContentMapper.selectByExample(null);
        return tbContents;
    }

    @Override
    public boolean insert(TbContent tbContent) {
        int i = tbContentMapper.insertSelective(tbContent);
        return i > 0;
    }

    @Override
    public TbContent selectById(Long id) {
        TbContent tbContent = tbContentMapper.selectByPrimaryKey(id);
        return tbContent;
    }

    @Override
    public boolean updateALl(TbContent tbContent) {
        int i = tbContentMapper.updateByPrimaryKey(tbContent);
        return i > 0;
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        int i = tbContentMapper.deleteByIds(ids);
        return i > 0;
    }

    @Override
    public List<TbContent> selectByLunBo(Integer id) {
        String key = "CONTENT";
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> contentList = (List<TbContent>) hash.get(key, feild);
            return contentList;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByLunBo();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByjinri(Integer id) {
        String key = "CONTENT" + id;
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> tbContents = (List<TbContent>) hash.get(key, feild);
            return tbContents;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByJinRi();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByLike(Integer id) {
        String key = "CONTENT" + id;
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> tbContents = (List<TbContent>) hash.get(key, feild);
            return tbContents;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByLike();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByYouQu(Integer id) {
        String key = "CONTENT" + id;
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> tbContents = (List<TbContent>) hash.get(key, feild);
            return tbContents;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByYouQu();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByFloor(Integer id) {
        String key = "CONTENT" + id;
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> tbContents = (List<TbContent>) hash.get(key, feild);
            return tbContents;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByFloor();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByFloor2(Integer id) {
        String key = "CONTENT" + id;
        String feild = "" + id;
        HashOperations hash = redisTemplate.opsForHash();
        Boolean b = hash.hasKey(key, feild);
        if (b) {
            List<TbContent> tbContents = (List<TbContent>) hash.get(key, feild);
            return tbContents;
        } else {
            List<TbContent> tbContents = tbContentMapper.selectByFloor2();
            hash.put(key, feild, tbContents);
            return tbContents;
        }
    }
}
